package com.sy.muldatasource.config;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import javax.sql.XADataSource;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.jta.JtaTransactionManager;
import com.atomikos.icatch.jta.UserTransactionImp;
import com.atomikos.icatch.jta.UserTransactionManager;

@Configuration
public class DataSourceConfig {
	private Context ctx = null;
	private Context envCtx = null;
	private Context jdbcCtx = null;
	
	public DataSourceConfig() throws NamingException {
		ctx = new InitialContext();
		envCtx = (Context) ctx.lookup("java:comp/env");
		jdbcCtx = (Context) envCtx.lookup("jdbc");//如果直接写java:comp/env/jdbc/one;再次调用.lookup("java:comp/env/jdbc/two")会报错。因此拆分调用
	}
	
	 //分布式事务管理器
    @Bean(name = "atomikosTransactionManager", initMethod = "init", destroyMethod = "close")
    public TransactionManager atomikosTransactionManager() throws Throwable {
        UserTransactionManager userTransactionManager = new UserTransactionManager();
        userTransactionManager.setForceShutdown(true);
        return userTransactionManager;
    }
    
    //事务
    @Bean(name = "userTransaction")
    public UserTransaction userTransaction() throws Throwable {
        UserTransactionImp userTransactionImp = new UserTransactionImp();
        return userTransactionImp;
    }
    
    @Bean(name = "transactionManager")
    @DependsOn({ "userTransaction", "atomikosTransactionManager" })
    public PlatformTransactionManager transactionManager() throws Throwable {
        UserTransaction userTransaction = userTransaction();
        TransactionManager atomikosTransactionManager = atomikosTransactionManager();
        return new JtaTransactionManager(userTransaction, atomikosTransactionManager);
    }

	
    @Primary
	@Bean("dataSourceOne")
	@DependsOn({"transactionManager"})//指定事务管理器，解决报warn:transaction manager not running提示问题
	public DataSource firstDataSource() throws IllegalArgumentException, NamingException {
		AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
		Object lookupXADB = jdbcCtx.lookup("one");
		XADataSource xa_ds = (XADataSource) lookupXADB;
		atomikosDataSourceBean.setXaDataSource(xa_ds);
		atomikosDataSourceBean.setUniqueResourceName("oneDataSource");
		return atomikosDataSourceBean;
	}

	@Bean("dataSourceTwo")
	@DependsOn({"transactionManager"})
	public DataSource secondDataSource() throws IllegalArgumentException, NamingException {
		AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
		Object lookupXADB = jdbcCtx.lookupLink("two");
		XADataSource xa_ds = (XADataSource) lookupXADB;
		atomikosDataSourceBean.setXaDataSource(xa_ds);
		atomikosDataSourceBean.setUniqueResourceName("twoDataSource");
		return atomikosDataSourceBean;
	}

}
